package com.ken.work.apache.mina;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.http.api.DefaultHttpResponse;
import org.apache.mina.http.api.HttpRequest;
import org.apache.mina.http.api.HttpResponse;
import org.apache.mina.http.api.HttpStatus;
import org.apache.mina.http.api.HttpVersion;


public class HttpServerHandler  extends MyIoHandlerAdapter {
	private static Logger log = Logger.getLogger(HttpServerHandler.class);

	@Override
	public void messageReceived(IoSession session, Object message)
			throws Exception {
		// TODO Auto-generated method stub
 
		//解码器将请求转换成HttpRequest对象
		try {
			HttpRequest request = (HttpRequest) message;
			String requestPath=request.getRequestPath().trim();
			log.info("请求路径："+requestPath);
			if("/favicon.ico".equals(requestPath)){
				session.write(getNullResponse());
				return;
			}else {
				MyHttpResponse myHttpResponse=getHtmlResponse(requestPath);
				session.write(myHttpResponse.getResponse());
				session.write(myHttpResponse.getIoBuffer());
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
        
		
	 

	}
 
	
 
}
